# -*- encoding=utf8 -*-
__author__ = "vigor"

from airtest.core.api import *
import time,random



def randomSleep(a,b):
    """
                        随机获取a，b间一个值 i，
    a小于等于i小于等于b，强制休眠 i毫秒
                        默认休眠100毫s
    """
    i=100
    if a <= b and a >=0 :
        i = random.randint(a,b)/1000
    time.sleep(i)
    print('sleep time: '+ str(i)+ 's')
    return i

auto_setup(__file__)

sleep_min_time = 200
sleep_max_time = 300

once_safe_run_times = 500 #安全运行次数，大于这个次数强制退出
once_safe_run_count=0  #单次游戏内的空运行次数——没有发现按钮

max_run_times = 1  #游戏目标运行次数
total_run_count=0  #游戏内的实际运行次数

runflag = True
i = 0
while runflag  :
    #randomSleep(sleep_min_time,sleep_max_time)
    once_safe_run_count+=1
    if once_safe_run_count > once_safe_run_times :
        runflag = False
        print("waiting too long ,Task withdrawal,once_safe_run_count :"+str(once_safe_run_count))
        break
    
    #游戏中跳过出现的次数较多，要多检查，按照2/8原则分配检查图片的次数
    i+=1
    if i%5 != 0 :
        if exists(Template(r"tpl1614090416557.png", threshold=0.75, record_pos=(0.428, -0.212), resolution=(2342, 1080))) : #存在【跳过】图片
            #touch pitcure
            touch(Template(r"tpl1614135888566.png", threshold=0.75, rgb=False, record_pos=(0.432, -0.212), resolution=(2342, 1080)))

            #空运行次数清零
            once_safe_run_count=0

        continue

    if exists(Template(r"tpl1614087431124.png", record_pos=(0.001, 0.184), resolution=(2342, 1080))) : #存在【点击屏幕继续】图片
        #touch pitcure
        touch(Template(r"tpl1614087431124.png", record_pos=(0.001, 0.184), resolution=(2342, 1080)))
        randomSleep(sleep_min_time,sleep_max_time)

        #空运行次数清零
        once_safe_run_count=0
        continue        
        
    if  exists(Template(r"tpl1614089992648.png", record_pos=(0.348, 0.192), resolution=(2342, 1080))) : #存在【再次挑战】图片
        randomSleep(sleep_min_time,sleep_max_time)
        #touch pitcure
        touch(Template(r"tpl1614089992648.png", record_pos=(0.348, 0.192), resolution=(2342, 1080)))
        #计算总运行次数
        total_run_count+=1
        #空运行次数清零
        once_safe_run_count=0
        continue
  
    if  total_run_count >= max_run_times  :
        runflag = False
        print("Task is finished,total run " + str(total_run_count)+" times")
        break

    if exists(Template(r"tpl1614087457846.png", record_pos=(0.26, 0.151), resolution=(2342, 1080))) : #存在【闯关】图片
        #touch pitcure
        touch(Template(r"tpl1614087457846.png", record_pos=(0.26, 0.151), resolution=(2342, 1080)))
        #空运行次数清零
        once_safe_run_count=0
        continue
        


# 退出游戏        
while True  :
    if exists(Template(r"tpl1614103829433.png", record_pos=(0.287, 0.189), resolution=(2342, 1080))) :
        touch(Template(r"tpl1614103829433.png", record_pos=(0.287, 0.189), resolution=(2342, 1080)))
        randomSleep(sleep_min_time,sleep_max_time)
        if exists(Template(r"tpl1614105113822.png", record_pos=(0.075, 0.095), resolution=(2342, 1080))) :
            touch(Template(r"tpl1614105113822.png", record_pos=(0.075, 0.095), resolution=(2342, 1080)))
            break
       
    if exists(Template(r"tpl1614104376368.png", record_pos=(0.189, 0.191), resolution=(2342, 1080))) :
        touch(Template(r"tpl1614104376368.png", record_pos=(0.189, 0.191), resolution=(2342, 1080)))
        randomSleep(sleep_min_time,sleep_max_time)
        continue        

    if exists(Template(r"tpl1614103654093.png", record_pos=(-0.418, -0.205), resolution=(2342, 1080))) :
        touch(Template(r"tpl1614103654093.png", record_pos=(-0.418, -0.205), resolution=(2342, 1080)))
        randomSleep(sleep_min_time,sleep_max_time)
        continue
    
    if exists(Template(r"tpl1614103747423.png", record_pos=(0.389, -0.204), resolution=(2342, 1080))) :
        touch(Template(r"tpl1614103747423.png", record_pos=(0.389, -0.204), resolution=(2342, 1080)))
        randomSleep(sleep_min_time,sleep_max_time)
        continue


 







